import React from 'react';
import { withRouter } from 'react-router'
import PropTypes from 'prop-types';
import getUserInfo from '../../../request/get_user_info';
import './index.scss';

class MyInfo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      user: '',
    }
  }
  componentDidMount() {
    this.getMyInfo()
  }
  // 获取的我的个人信息
  getMyInfo() {
    const params = {
      include: this.props.userInfo.user_id,
    };
    // 请求数据
    getUserInfo(params).then((res) => {
      const json = res.data
      if (json && json.length) {
        this.setState({
          user: json[0],
        })
      } else {
        // 请求不到数据跳转到登录页面
        this.props.history.push(`/user/login/${encodeURIComponent(this.props.match.url)}`);
      }
    })
  }
  render() {
    return (
      <div className="my-info">
        { this.state.user ?
          <div className="info-content">
            <div className="userid"><span>用户ID:</span>{this.state.user.id}</div>
            <div className="nickname"><span>用户昵称:</span>{this.state.user.name}</div>
            <div className="description"><span>用户介绍:</span>{this.state.description ? this.state.description : '你还没有任何进行任何描述...'}</div>
          </div> : ''
        }
      </div>
    )
  }
}
export default withRouter(MyInfo);
MyInfo.propTypes = {
  userInfo: PropTypes.objectOf(Object),
  history: PropTypes.objectOf(Object),
  match: PropTypes.objectOf(Object),
}
